package com.whc.gulimall.order.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextListener;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * Rabbit相关配置
 *
 * @author angaoen
 */
@Configuration
@Slf4j
public class MyRabbitConfig {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @PostConstruct
    public void initRabbitTemplate() {
        rabbitTemplate.setConfirmCallback((correlationData, ack, cause)
                -> log.info("confirm({} correlationData, {} ack, {} cause)", correlationData, ack, cause));

        rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey)
                -> log.error("returnedMessage({} message, {} replyCode, {} replyText, {} exchange, {} routingKey)",
                message, replyCode, replyText, exchange, routingKey));
    }

}
